﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GMS.BIZ;
using System.Collections;
using System.Data;

namespace Web.Information
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            //初始化学生公司信息    
            if (!((Account)Session["Account"]).RoleCurrent.Equals('s')) Response.Redirect("Default.aspx");
           
            Student myStudent = (Student)Session["Student"];
            if (myStudent.StudentName == "")
                Response.Redirect("StuInformation.aspx");
            GridView_Company.DataSource = myStudent.Companys.GetDataSet();
            GridView_Company.DataBind();
          
            //绑定DropDownList数据    
            if (!IsPostBack)
                {
                    ArrayList arraylist = new ArrayList();
                    arraylist = Library.GetCompanyStyleLib();
                    for (int i = 0; i < arraylist.Count; i++)
                    {
                        ListItem myListItem = new ListItem();
                        myListItem = (ListItem)arraylist[i];
                        DropDownList_comstyle.Items.Add(myListItem);
                    }
                    ListItem otherListItem2 = new ListItem();
                    otherListItem2.Text = "无";
                    otherListItem2.Value = "100";
                    DropDownList_comstyle.Items.Add(otherListItem2);
                
                    ArrayList arraylist2 = new ArrayList();
                    arraylist2 = Library.GetCompanyLib();
                    for (int i = 0; i < arraylist2.Count; i++)
                    {
                        ListItem myListItem = new ListItem();
                        myListItem = (ListItem)arraylist2[i];
                        DropDownList_comname.Items.Add(myListItem);
                    }

                    ListItem otherListItem = new ListItem();
                    otherListItem.Text = "其它";
                    otherListItem.Value = "others";
                    DropDownList_comname.Items.Add(otherListItem); 
                }

            if (DropDownList_comname.SelectedValue == "others")
            {
                Label_nolist.Visible = true;
                TextBox_nolist.Visible = true;
                RequiredFieldValidator_nolist.Enabled = true;
            }
            else
            {
                Label_nolist.Visible = false;
                TextBox_nolist.Visible = false;
                RequiredFieldValidator_nolist.Enabled = false;
            }
            #region ViewState

            //初始化ViewState       
            if (ViewState["status"] == null)
                ViewState["status"] = 1;
            
            this.ViewStateControls(int.Parse(ViewState["status"].ToString()));

            #endregion ViewState
        }

        //设置ViewState对应的添加、修改、删除三种状态
        protected void ViewStateControls(int i)
        {
            if (i == 1)//添加
            {
                ImageButton_Cancel.Visible = false;
                ImageButton_Add.Visible = true;
                ImageButton_Edit.Visible = false;
                ImageButton_Delete.Visible = false;
            }
            else if (i == 2)//修改
            {
                ImageButton_Cancel.Visible = true;
                ImageButton_Add.Visible = false;
                ImageButton_Edit.Visible = true;
                ImageButton_Delete.Visible = false;
            }
            else if (i == 3) //删除
            {
                ImageButton_Cancel.Visible = true;
                ImageButton_Add.Visible = false;
                ImageButton_Edit.Visible = false;
                ImageButton_Delete.Visible = true; 
            }
        }

        //GridView的修改按钮
        protected void GridViewButton_Edit_Click(object sender, ImageClickEventArgs e)
        {
            Company myCompany = new Company();
            Student myStudent = (Student)Session["Student"];
            myCompany = myStudent.Companys.GetOne(decimal.Parse(((ImageButton)sender).CommandArgument));
            Label_CompanyID.Text = myCompany.id.ToString();
            
            DropDownList_comstyle.SelectedValue = myCompany.com_style.ToString();
            TextBox_comaddress.Text = myCompany.com_address.ToString();
            TextBox_comtel.Text = myCompany.com_tel.ToString();
            TextBox_compost.Text = myCompany.com_post.ToString();
            TextBox_comweb.Text = myCompany.com_web.ToString();
            if (myCompany.ifSLD == 1)
                CheckBox_ifSLD.Checked = true;
            else
                CheckBox_ifSLD.Checked = false;
            
            

            ViewState["status"] = 2;
            Label_result.Text = "";
            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = myCompany.com_name +" "+ Company.GetCompanyStyleName( int.Parse(myCompany.com_style.ToString()));
        }

        //GridView的删除按钮
        protected void GridViewButton_Delete_Click(object sender, ImageClickEventArgs e)
        {
            Company myCompany = new Company();
            Student myStudent = (Student)Session["Student"];
            myCompany = myStudent.Companys.GetOne(decimal.Parse(((ImageButton)sender).CommandArgument));
            Label_CompanyID.Text = myCompany.id.ToString();
           
            DropDownList_comstyle.SelectedValue = myCompany.com_style.ToString();
            TextBox_comaddress.Text = myCompany.com_address.ToString();
            TextBox_comtel.Text = myCompany.com_tel.ToString();
            TextBox_compost.Text = myCompany.com_post.ToString();
            TextBox_comweb.Text = myCompany.com_web.ToString();
            if (myCompany.ifSLD == 1)
                CheckBox_ifSLD.Checked = true;
            else
                CheckBox_ifSLD.Checked = false;

            ViewState["status"] = 3;
            Label_result.Text = "";
            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = myCompany.com_name + " " + Company.GetCompanyStyleName(int.Parse(myCompany.com_style.ToString()));
        }
        
        //修改按钮
        protected void ImageButton_Edit_Click(object sender, ImageClickEventArgs e)
        {
            Company myCompany = new Company();
            Student myStudent1 = (Student)Session["Student"];
            myCompany.id = decimal.Parse(Label_CompanyID.Text.ToString());
            if (TextBox_nolist.Text == null || TextBox_nolist.Text == string.Empty)
                myCompany.com_name = DropDownList_comname.SelectedValue;
            else
            {
                myCompany.com_name = TextBox_nolist.Text.ToString();
            }
            myCompany.com_style = int.Parse(DropDownList_comstyle.SelectedValue);
            myCompany.com_address = TextBox_comaddress.Text.ToString();
            myCompany.com_tel = TextBox_comtel.Text.ToString();
            myCompany.com_post = TextBox_compost.Text.ToString();
            myCompany.com_web = TextBox_comweb.Text.ToString();
            if (CheckBox_ifSLD.Checked)
                myCompany.ifSLD = 1;
            else
                myCompany.ifSLD = 0;
            //判断当前公司记录四联单为1时，置其他公司记录四联单为0
            if (myCompany.ifSLD == 1)
            {
                DataSet ds = myStudent1.Companys.GetDataSet();
                //当该学生公司列表不为空时
                if (ds != null)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        Company myExitCompany = new Company();
                        myExitCompany = myStudent1.Companys.GetOne(decimal.Parse(ds.Tables[0].Rows[i]["id"].ToString()));
                        myExitCompany.ifSLD = 0;
                        myStudent1.Companys.Update(myExitCompany);       
                    }
                }
            }
            //更新该学生该条公司信息
            string str = myStudent1.Companys.Update(myCompany);
            //判断当前公司记录四联单为1时，置其他公司记录四联单为0
            if (myCompany.ifSLD == 1)
            { 
                DataSet ds= myStudent1.Companys.GetDataSet();
                //当该学生公司列表不为空时
                if (ds != null)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    { 
                        }
                }
            }
            //判断更新是否成功
            if ((str.CompareTo(Resources.ErrorMessage.OK)) == 0)
            {
                Label_result.Text = Resources.ErrorMessage.OK;
                Student myStudent2 = (Student)Session["Student"];
                GridView_Company.DataSource = myStudent2.Companys.GetDataSet();
                GridView_Company.DataBind();
            }
            else Label_result.Text = Resources.ErrorMessage.DBOptError;

            ViewState["status"] = 1;

            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = "";
            
        }


        //删除按钮
        protected void ImageButton_Delete_Click(object sender, ImageClickEventArgs e)
        {
            Student myStudent1 = (Student)Session["Student"];
            string str = myStudent1.Companys.Delete(decimal.Parse(Label_CompanyID.Text.ToString()));
            //判断删除是否成功
            if ((str.CompareTo(Resources.ErrorMessage.OK)) == 0)
            {
                Label_result.Text = Resources.ErrorMessage.OK;
                Student myStudent2 = (Student)Session["Student"];
                GridView_Company.DataSource = myStudent2.Companys.GetDataSet();
                GridView_Company.DataBind();
            }
            else Label_result.Text = Resources.ErrorMessage.DBOptError;

            ViewState["status"] = 1;
            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = "";
        }

        //添加按钮
        protected void ImageButton_Add_Click(object sender, ImageClickEventArgs e)
        {
            Company myCompany = new Company();
            Student myStudent1 = (Student)Session["Student"];
            if (TextBox_nolist.Text == null || TextBox_nolist.Text == string.Empty)
                myCompany.com_name = DropDownList_comname.SelectedValue;
            else
            {
                myCompany.com_name = TextBox_nolist.Text.ToString();
            }
            if (int.Parse(DropDownList_comstyle.SelectedValue) != 100)
            {
                myCompany.com_style = int.Parse(DropDownList_comstyle.SelectedValue);
                myCompany.com_address = TextBox_comaddress.Text.ToString();
                myCompany.com_tel = TextBox_comtel.Text.ToString();
                myCompany.com_post = TextBox_compost.Text.ToString();
                myCompany.com_web = TextBox_comweb.Text.ToString();
                if (CheckBox_ifSLD.Checked)
                    myCompany.ifSLD = 1;
                else myCompany.ifSLD = 0;
                //判断当前公司记录四联单为1时，置其他公司记录四联单为0
                if (myCompany.ifSLD == 1)
                {
                    DataSet ds = myStudent1.Companys.GetDataSet();
                    //当该学生公司列表不为空时
                    if (ds != null)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            Company myExitCompany = new Company();
                            myExitCompany = myStudent1.Companys.GetOne(decimal.Parse(ds.Tables[0].Rows[i]["id"].ToString()));
                            myExitCompany.ifSLD = 0;
                            myStudent1.Companys.Update(myExitCompany);
                        }
                    }
                }
                //添加该学生的一条公司信息
                string str = myStudent1.Companys.Add(myCompany);
                //判断添加是否成功
                if ((str.CompareTo(Resources.ErrorMessage.OK)) == 0)
                {
                    Label_result.Text = Resources.ErrorMessage.OK;
                    Student myStudent2 = (Student)Session["Student"];
                    GridView_Company.DataSource = myStudent2.Companys.GetDataSet();
                    GridView_Company.DataBind();
                }
                else Label_result.Text = Resources.ErrorMessage.DBOptError;

                ViewState["status"] = 1;

                Label_nolist.Visible = false;
                TextBox_nolist.Visible = false;
                TextBox_nolist.Text = "";
                Label_EditInformation2.Text = "";
            }
            else Label_result.Text = "公司类型为空，无法添加";
        }

        //取消按钮
        protected void ImageButton_Cancel_Click(object sender, ImageClickEventArgs e)
        {
           
            TextBox_comaddress.Text = null;
            TextBox_comtel.Text = null;
            TextBox_compost.Text = null;
            TextBox_comweb.Text = null;
            CheckBox_ifSLD.Checked = false;
            ViewState["status"] = 1;

            Label_nolist.Visible = false;
            TextBox_nolist.Visible = false;
            TextBox_nolist.Text = "";
            Label_EditInformation2.Text = "";
        }

        protected void GridView_Company_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                switch (int.Parse(e.Row.Cells[1].Text))
                {
                    case 1: e.Row.Cells[1].Text = Company.GetCompanyStyleName(1); break;
                    case 2: e.Row.Cells[1].Text = Company.GetCompanyStyleName(2); break;
                    case 3: e.Row.Cells[1].Text = Company.GetCompanyStyleName(3); break;
                    case 4: e.Row.Cells[1].Text = Company.GetCompanyStyleName(4); break;
                    default: break;
                }
                switch (int.Parse(e.Row.Cells[6].Text))
                {
                    case 1: e.Row.Cells[6].Text = "是"; break;
                    case 0: e.Row.Cells[6].Text = "否"; break;
                    default: break;
                }
            }
        }

        protected void DropDownList_comname_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList_comname.SelectedValue == "others")
            {
                Label_nolist.Visible = true;
                TextBox_nolist.Visible = true;
                RequiredFieldValidator_nolist.Enabled = true;
            }
            else
            {
                Label_nolist.Visible = false;
                TextBox_nolist.Visible = false;
                RequiredFieldValidator_nolist.Enabled = false;
            }
        }

        
    }
}
